package beautiful.butterfly.schedule.task_manager;

public class Lock {

    private Object object = new Object();
    private int waitThreadCount = 0;

    public Lock() {
    }

    //
    public void currentThreadWait() throws Exception {
        synchronized (object) {
            this.object.wait();
        }
    }

    public void notifyOtherWaitThread() throws Exception {
        synchronized (object) {
            this.object.notifyAll();
        }
    }
    //

    public void addWaitThread() {
        synchronized (this) {
            waitThreadCount = waitThreadCount + 1;
        }
    }

    public void realseThread() {
        synchronized (this) {
            waitThreadCount = waitThreadCount - 1;
        }
    }

    public boolean realseThreadAndReturnIsLastThread() {
        synchronized (this) {
            if (this.waitThreadCount == 1) {
                return false;
            } else {
                waitThreadCount = waitThreadCount - 1;
                return true;
            }
        }
    }


}
